package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.commons.tool.exception.ExceptionUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;
    @Autowired
    private ProductConverter productConverter;
    @Autowired
    private PanelMapper panelMapper;
    @Autowired
    private PanelContentMapper panelContentMapper;
    @Autowired
    private ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            request.requestCheck();
            Long id = request.getId();
            Item item = itemMapper.selectByPrimaryKey(id);
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
            ProductDetailDto productDetailDto = productConverter.item2DetailDto(item);
            String detail;
            if (itemDesc != null && StringUtils.hasText(detail = itemDesc.getItemDesc())) {
                productDetailDto.setDetail(detail);
            }
            response.setProductDetailDto(productDetailDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse = new AllProductResponse();

        try {
            // 执行参数校验
            request.requestCheck();

            String sort;
            if (request.getSort() == null) {
                sort = "asc";
            } else if (request.getSort().equals("1")) {
                sort = "asc";
            } else {
                sort = "desc";
            }

            PageHelper.startPage(request.getPage(), request.getSize());
            List<Item> items = itemMapper.selectItemFront(request.getCid(), "price", sort, request.getPriceGt(), request.getPriceLte());
            List<ProductDto> productDtos = productConverter.items2Dto(items);

            PageInfo<Item> pageInfo = new PageInfo<>(items);
            long total = pageInfo.getTotal();

            allProductResponse.setProductDtoList(productDtos);
            allProductResponse.setTotal(total);
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            return allProductResponse;

        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);
        }

        return allProductResponse;
    }


    //查询推荐商品
    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();

        try {
            // 在内容分类表 tb_panel 中查询推荐商品的内容分类
            List<Panel> panels = panelMapper.selectPanelContentById(GlobalConstants.RECOMMEND_PANEL_ID);
            Set<PanelDto> panelDtoSet = new HashSet<>();

            for (Panel panel : panels) {
                PanelDto panelDto = contentConverter.panel2Dto(panel);
                //通过 PanelId 查询 tb_panel_content 的内容
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                panelDto.setPanelContentItems(panelContentItemDtos);
                panelDtoSet.add(panelDto);
            }
            recommendResponse.setPanelContentItemDtos(panelDtoSet);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("IProductServiceImpl.getRecommendGoods occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse, e);
        }

        return recommendResponse;
    }
}
